<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
class Admin_Model_User{
	// class constants
	const STRING_LENGTH_NAME 		= 254;
	const STRING_LENGTH_PASSWORD 	= 254;
	const STRING_LENGTH_EMAIL 		= 254;

	// class data
	private $id;
	private $email;
	private $name;
	private $password;
	private $username;

	//other properites
	private $mapper;

	/**
	 * Class constructor
	 * @param mixed $construct If an int is given, id is set, if an array is given, __set is called
	 */
	public function __construct( $construct = null ){
		if( is_numeric( $construct ) ){
			$this->setId( $construct );
		}
		else if( is_array( $construct ) || is_object( $construct ) ){
			foreach( $construct as $key => $value ){
				$this->__set( $key, $value );
			}
		}
	}

	/**
	 * Magic setter
	 * Any property may be set trough this function. Will call 'set$password' function
	 * if this class defines one.
	 *
	 * @param string $password Name of the property
	 * @param mixed $value Value for property
	 * @return void
	 */
	public function __set( $name, $value ){
		if( property_exists( $this, $name ) ){
			if( ( $method = 'set' . ucfirst( $name ) ) && method_exists( $this, $method ) ){
				$value = (array) $value;
				call_user_func_array( array( $this, $method ), $value );
			}
			else{
				$this->$name = $value;
			}
		}
	}

	/**
	 * Magic getter
	 * Tries to call class' getter if defined
	 *
	 * @param string $name Name for the property
	 * @return mixed $value or NULL
	 */
	public function __get( $name ){
		if( property_exists( $this, $name ) ){
			if( ( $method = 'get' . ucfirst( $name ) ) && method_exists( $this, $name ) ){
				return $this->$method();
			}
			else if( null == $this->$name ){
				$this->find();

			}
			return $this->$name;
		}
	}

	/**
	 * Sets ID and asserts it's an int
	 */
	public function setId( $id ){
		$this->id = (int) $id;
	}

	/**
	 * set password: assert length
	 */
	public function setPassword( $password ){
		$this->password = md5($password);
	}

	/**
	 * set name: assert length
	 */
	public function setName( $name ){
		if( strlen( $name ) > self::STRING_LENGTH_NAME ){
			return;
		}

		$this->name = $name;
	}

	/**
	 * set email: assert length
	 */
	public function setEmail( $email ){
		if( strlen( $email ) > self::STRING_LENGTH_EMAIL ){
			return;
		}

		$this->email = $email;
	}

	public function fetchAll(){
		return $this->getMapper()->fetchAll();
	}

	public function save(){
		$this->getMapper()->save( $this );
	}

	/**
	 * instantiate from database
	 *
	 * If id is set, use find.
	 * If name is set, use findByName
	 *
	 * @return void
	 */
	private function find(){
		if( null !== $this->id ){
			$this->getMapper()->find( $this );
		}
		else if( null !== $this->name ){
			$this->getMapper()->findByName( $this );
		}
	}

    /**
     * Fetch or instantiates the mapper
     *
     * @return object default_model_mapper
     */
    private function getMapper(){
        if( $this->mapper === null ){
            $mapper = new Admin_Model_UserMapper();
            $this->setMapper( $mapper    );
        }
        return $this->mapper;
    }

    /**
     * Set mapper
     *
     * @param default_model_mapper $mapper
     * @return Admin_Model_User $this fluent interface
     */
    private function setMapper( Admin_Model_UserMapper $mapper ){
        $this->mapper = $mapper;
        return $this;
    }
}
